package com.xuecheng.manage_course.controller;

import com.xuecheng.api.course.CourseControllerApi;
import com.xuecheng.framework.domain.course.*;
import com.xuecheng.framework.domain.course.ext.CourseView;
import com.xuecheng.framework.domain.course.ext.TeachplanNode;
import com.xuecheng.framework.domain.course.request.CourseListRequest;
import com.xuecheng.framework.domain.course.response.CoursePublishResult;
import com.xuecheng.framework.model.response.QueryResponseResult;
import com.xuecheng.framework.model.response.ResponseResult;
import com.xuecheng.framework.utils.Oauth2Util;
import com.xuecheng.framework.utils.XcOauth2Util;
import com.xuecheng.framework.web.BaseController;
import com.xuecheng.manage_course.service.CoursePicService;
import com.xuecheng.manage_course.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController extends BaseController implements CourseControllerApi {

    @Autowired
    CourseService courseService;
    @Autowired
    CoursePicService coursePicService;

    //查询课程计划
    @Override
    @GetMapping("/teachplan/list/{courseId}")
    public TeachplanNode findTeachplanList(@PathVariable("courseId") String courseId) {
        return courseService.findTeachplanList(courseId);
    }

    /**
     * 添加teachPlan
     * @param teachplan
     * @return
     */
    @Override
    @PostMapping("/teachplan/add")
    public ResponseResult addTeachplan(@RequestBody Teachplan teachplan) {
        return courseService.addTeachplan(teachplan);
    }

    /**
     * 查询课程列表
     * @param page
     * @param size
     * @param courseListRequest
     * @return
     */
    @Override
    @GetMapping("/coursebase/list/{page}/{size}")
    public QueryResponseResult findCourseList(@PathVariable("page") int page,
                                              @PathVariable("size") int size,
                                              CourseListRequest courseListRequest) {
        //从请求头中获取jwt令牌, 取出用户所属的companyId
        Map<String, String> jwt = Oauth2Util.getJwtClaimsFromHeader(request);
        String companaId = jwt.get("companyId");
        courseListRequest.setCompanyId(companaId);
        return courseService.findCourseList(page, size, courseListRequest);
    }


    /**
     * 新增coursebase
     * @param courseBase
     * @return
     */
    @Override
    @PostMapping("/coursebase/add")
    public ResponseResult addCourse(@RequestBody CourseBase courseBase) {
        return courseService.addCourse(courseBase);
    }

    /**
     * 修改课程
     * @param courseBase
     * @return
     */
    @Override
    @PutMapping("/coursebase/update")
    public ResponseResult updateCourse(@RequestBody CourseBase courseBase) {
        return courseService.addCourse(courseBase);
    }

    /**
     * 根据id查询coursebase
     * @param courseid
     * @return
     */
    @Override
    @PreAuthorize("hasAuthority('course_get_baseinfo')")
    @GetMapping("/findById/{courseid}")
    public CourseBase findById(@PathVariable("courseid") String courseid) {
        return courseService.findById(courseid);
    }

    /**
     * 根据courseid查询courseMarket信息
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("/courseMarket/get/{courseId}")
    public CourseMarket findByCourseId(@PathVariable("courseId") String courseId) {
        return courseService.findCourseMarketByCourseId(courseId);
    }

    /**
     * 修改课程营销信息
     * @param courseid
     * @param courseMarket
     * @return
     */
    @Override
    @PutMapping("/courseMarket/update/{courseid}")
    public ResponseResult updateCourseMarket(@PathVariable("courseid") String courseid,
                                             @RequestBody CourseMarket courseMarket) {
        return courseService.updateCourseMarket(courseid, courseMarket);
    }

    /**
     * 新增课程图片
     * @param courseId
     * @param pic
     * @return
     */
    @Override
    @PostMapping("/coursepic/add")
    public ResponseResult addCoursePic(String courseId, String pic) {
        return coursePicService.addCoursePic(courseId, pic);
    }

    /**
     * 查询课程图片
     * @param courseId
     * @return
     */
    @Override
    @PreAuthorize("hasAuthority('course_find_pic')")
    @GetMapping("/coursepic/list/{courseId}")
    public CoursePic findCoursePic(@PathVariable("courseId") String courseId) {
        return coursePicService.findCoursePic(courseId);
    }

    /**
     * 删除课程图片
     * @param courseId
     * @return
     */
    @Override
    @DeleteMapping("/coursepic/delete")
    public ResponseResult deletCoursePic(String courseId) {
        return coursePicService.deleteById(courseId);
    }

    /**
     * 课程视图查询
     * @param courseId
     * @return 课程视图页面 数据模型
     */
    @Override
    @GetMapping("/courseview/{id}")
    public CourseView courseview(@PathVariable("id") String courseId) {
        return courseService.getCourseView(courseId);
    }

    //课程预览
    @Override
    @PostMapping("/preview/{id}")
    public CoursePublishResult preview(@PathVariable("id") String id) {
        return courseService.preview(id);
    }

    @Override
    @PostMapping("/publish/{id}")
    public CoursePublishResult publish(@PathVariable("id") String courseId) {
        return courseService.publish(courseId);
    }

    //保存课程计划和媒资信息的关联
    @Override
    @PostMapping("/savemedia")
    public ResponseResult saveTeachPlanAndMedia(@RequestBody TeachplanMedia teachplanMedia) {
        return courseService.saveTeachPlanAndMedia(teachplanMedia);
    }
}
